import Mock from 'mockjs'

// belong: 0 为热点，1为娱乐.....
let list = [{
  title: "是风口还是泡沫：特斯拉要成为下一个“苹果”吗？",
  source:  "2020-07-13 05:45:50",
  comment: "新浪财经",
  time: 0,
  imgUrl: "https://n.sinaimg.cn/finance/transform/612/w362h250/20200713/6161-iwhseit7688192.jpg",
  showType: 1,
  belong: 1,
  href: "https://finance.sina.cn/usstock/mggd/2020-07-13/detail-iivhvpwx5008956.d.html?vt=4&pos=108"
},
{
  title: "日媒：疫情下多国争相采购 中国口罩厂商不断适应全球需求",
  source:  "2020-07-13 06:03:00",
  comment: "环球网",
  time: 0,
  imgUrl: "https://n.sinaimg.cn/default/feedbackpics/transform/116/w550h366/20180517/q7Vh-harvfhu4569176.png",
  showType: 1,
  belong: 1,
  href: "https://news.sina.cn/gn/2020-07-13/detail-iivhuipn2633037.d.html?vt=4&pos=108"
},
{
  title: "理想汽车拟赴美上市 两张牌还须打好",
  source:  "2020-07-13 04:43:00",
  comment: "新京报",
  time: 0,
  imgUrl: "https://n.sinaimg.cn/tech/transform/667/w400h267/20200713/4412-iwhseit7660115.jpg",
  showType: 1,
  belong: 1,
  href: "https://tech.sina.cn/i/gn/2020-07-13/detail-iivhvpwx5006714.d.html?vt=4&pos=108"
},
{
  title: "App窃听？信息裸奔？App专项治理工作组这样��管",
  source: "2020-07-13 05:06:00",
  comment:"第一财经",
  time: 0,
  imgUrl: "https://n.sinaimg.cn/tech/transform/667/w400h267/20200713/6274-iwhseit7670245.jpg",
  showType: 1,
  belong: 1,
  href: "https://tech.sina.cn/i/gn/2020-07-13/detail-iivhuipn2630743.d.html?vt=4&pos=108"
},
{
  title: "6人诈骗苹果公司获刑：买手机拆卸主板出售植入旧主板再退货",
  source: "2020-07-13 05:21:50",
  comment:"新浪科技综合",
  time: 0,
  imgUrl: "https://n.sinaimg.cn/tech/transform/667/w400h267/20200713/4ab6-iwhseit7678628.jpg",
  showType: 1,
  belong: 1,
  href: "https://tech.sina.cn/apple/2020-07-13/detail-iivhuipn2631271.d.html?vt=4&pos=108"
},
{
  title: "土耳其宣布将圣索菲亚大教堂改为清真寺，引发欧美俄众怒",
  source:"2020-07-12 22:41:03",
  comment:"观察者网",
  time: 0,
  imgUrl: "https://n.sinaimg.cn/sinakd2020712s/287/w697h390/20200712/3429-iwhseit7319122.png",
  showType: 1,
  belong: 1,
  href: "https://news.sina.cn/gj/2020-07-13/detail-iivhvpwx4990972.d.html?vt=4&pos=108"
},
{
  title: "广州南站到达旅客需要被隔离14天? 此消息系谣言",
  source:"2020-02-19 12:59:32",
  comment:"南方plus",
  time: 0,
  imgUrl: "https://cms-bucket.ws.126.net/2020/0219/d44ed681p00q5xn5g00d7c000s600e3c.png",
  showType: 1,
  belong: 1,
  href: "http://3g.163.com/news/20/0219/12/F5OHDT5R0001899O.html"
},
{
  title: "云南新冠肺炎新增死亡病例1例 无新增确诊病例",
  source:"2020-02-19 13:02:11",
  comment:"云南卫健委",
  time: 0,
  imgUrl: "https://cms-bucket.ws.126.net/2020/0219/5c034e92p00q5xnea00ktc000s600e3c.png",
  showType: 1,
  belong: 1,
  href: "http://3g.163.com/news/20/0219/13/F5OHIO7J0001899O.html"
},
{
  title: "美媒：美国深陷新冠肺炎疫情旋涡 束手无策难以摆脱",
  source:"2020-07-12 18:02:00",
  comment:"央视",
  time: 0,
  imgUrl: "https://n.sinaimg.cn/default/feedbackpics/transform/116/w550h366/20180517/pqiN-harvfhu4568885.png",
  showType: 1,
  belong: 1,
  href: "https://news.sina.cn/gj/2020-07-12/detail-iivhuipn2584432.d.html?vt=4&pos=108"
},]

let newList = []
Mock.mock('/api/getvedio', function (options) {
  let obj = JSON.parse(options.body)
  console.log(obj)
  let startIndex = obj.pageId * 5 - 5
  let endIndex = obj.pageId * 5
  if(endIndex < list.length) {
    newList = list.slice(startIndex, endIndex)
  } else {
    newList = list.slice(startIndex, endIndex)
  }
  switch (obj.id) {
    case 0:
      return newList
    case 1:
      return newList.filter(item => {
        return item.belong === 1
      })
    case 2: // 娱乐
      return newList.filter(item => {
        return item.belong === 2
      })
    default:
      return newList
  }
})


// 搜索
Mock.mock('/api/search', function (options) {
  let obj = JSON.parse(options.body)
  let result = list.filter(item => {
    if (item.title.indexOf(obj.value) !== -1) {
      return item
    }
  })
  let data = {
    result: result,
    stat: 'OK'
  }
  return data
})


function json(str) {
  let arr = str.split('&')
  let obj = {}
  arr.forEach(item => {
    let key = item.split('=')[0]
    let value = item.split('=')[1]
    obj[key] = value
  })
  return obj
}
